package com.qin.controller;
import com.alibaba.fastjson.JSONObject;
import com.qin.entity.VerSysEnv;
import com.qin.enums.RetCode;
import com.qin.service.RelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("relationController")
@RequestMapping("/relation")
@ResponseBody
public class RelationController {

    @Autowired
    RelationService relationService;

    @RequestMapping(value="/queryVerSysEnvs", produces="application/json;charset=UTF-8")
    public String queryVerSysEnvs(@RequestBody String body){
//        System.out.println("web层：queryVerSysEnvs");
        System.out.println("接口调用：/ver/queryVerSysEnvs");

        //入参封装到实体类
        VerSysEnv verSysEnv = JSONObject.parseObject(body, VerSysEnv.class);
        System.out.println("接口入参：" + body);
        //调用service层
        String retString = relationService.queryVerSysEnvs(verSysEnv).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/insertVerSysEnv", produces="text/html;charset=UTF-8")
    public String insertVerSysEnv(@RequestBody String body){
//        System.out.println("web层：insertVerSysEnv");
        System.out.println("接口调用：/ver/insertVerSysEnv");

        //入参封装到实体类
        VerSysEnv verSysEnv = JSONObject.parseObject(body, VerSysEnv.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((verSysEnv.getVersion_name() == null) || ("".equals(verSysEnv.getVersion_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "version_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((verSysEnv.getSystem_name() == null) || ("".equals(verSysEnv.getSystem_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "system_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((verSysEnv.getEnvironment() == null) || ("".equals(verSysEnv.getEnvironment()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "environment不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = relationService.insertVerSysEnv(verSysEnv).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/deleteVerSysEnv", produces="text/html;charset=UTF-8")
    public String deleteVerSysEnv(@RequestBody String body){
//        System.out.println("web层：deleteVerSysEnv");
        System.out.println("接口调用：/ver/deleteVerSysEnv");

        //入参封装到实体类
        VerSysEnv verSysEnv = JSONObject.parseObject(body, VerSysEnv.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((verSysEnv.getId() == null) || ("".equals(verSysEnv.getId()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = relationService.deleteVerSysEnv(verSysEnv).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
